package com.dc.web;

import com.dc.po.Blog;
import com.dc.po.Tag;
import com.dc.service.BlogService;
import com.dc.service.TagService;
import com.dc.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TagShowController {
    @Autowired
    private TagService tagService;
    @Autowired
    private BlogService blogService;

    /*defaultValue –如果请求没有该值或它为空，则这是作为回退机制的默认值。*/
    @GetMapping("/tags/{id}")
    public String tags(@PathVariable("id") Long tagId,
                       @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize",required = false,defaultValue = "6") int pageSize,
                       Model model){
        List<Tag> tags = tagService.listTagTop(100);
        if(tagId==-1){
            tagId = tags.get(0).getId();
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "updateTime");
        Pageable pageable = PageRequest.of(pageNum-1, pageSize, sort);
        Page<Blog> blogs = blogService.listBlog(tagId, pageable);

        model.addAttribute("tags",tags);
        model.addAttribute("page",blogs);
        model.addAttribute("activeTagId",tagId);
        return "tags";
    }
}
